/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Tiec;

import BUS.MonAn_BUS;
import DTO.MonAn_DTO;
import java.awt.HeadlessException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Mr
 */
public class JIFThemMonAn extends javax.swing.JInternalFrame {
    String maMonAn;
    /**
     * Creates new form JIFThemMonAn
     */
    public JIFThemMonAn() throws Exception {
        initComponents();
        this.setSize(880, 560);
        showBangMA();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbMonAn = new javax.swing.JTable();
        btThemMA = new javax.swing.JButton();
        btCapNhatMA = new javax.swing.JButton();
        tfTenMA = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfDonGiaMA = new javax.swing.JTextField();
        btXoaMA = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("QUẢN LÝ MÓN ĂN");
        setToolTipText("");

        tbMonAn.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbMonAn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbMonAnMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tbMonAn);

        btThemMA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        btThemMA.setText("Thêm Món Ăn");
        btThemMA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemMAActionPerformed(evt);
            }
        });

        btCapNhatMA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Sync.png"))); // NOI18N
        btCapNhatMA.setText("Cập Nhật");
        btCapNhatMA.setEnabled(false);
        btCapNhatMA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatMAActionPerformed(evt);
            }
        });

        jLabel3.setText("Tên Món Ăn");

        jLabel4.setText("Đơn giá");

        tfDonGiaMA.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfDonGiaMA.setText("0");

        btXoaMA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Erase.png"))); // NOI18N
        btXoaMA.setText("Xóa");
        btXoaMA.setEnabled(false);
        btXoaMA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaMAActionPerformed(evt);
            }
        });

        btThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        btThoat.setText("Thoát");
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(btThemMA)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btCapNhatMA, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btXoaMA, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btThoat)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfTenMA, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfDonGiaMA, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 504, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfTenMA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(tfDonGiaMA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btThoat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btCapNhatMA)
                        .addComponent(btThemMA)
                        .addComponent(btXoaMA)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbMonAnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbMonAnMouseClicked
        // TODO add your handling code here:
        int row = tbMonAn.getSelectedRow();
        if (row >= 0) {
            btXoaMA.setEnabled(true);
            btCapNhatMA.setEnabled(true);
            maMonAn = (String) tbMonAn.getValueAt(row, 0);
            String tenMonAn = (String) tbMonAn.getValueAt(row, 1);
            String donGia = tbMonAn.getValueAt(row, 2).toString();
            tfTenMA.setText(tenMonAn);
            tfDonGiaMA.setText(donGia);
        }
    }//GEN-LAST:event_tbMonAnMouseClicked

    private void btThemMAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemMAActionPerformed
        // TODO add your handling code here:
        try {
            luuMonAn();
            showBangMA();
        } catch (Exception ex) {
            Logger.getLogger(JIFThemMonAn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btThemMAActionPerformed

    private void btCapNhatMAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatMAActionPerformed
        // TODO add your handling code here:
        String tenMonAn;
        tenMonAn = tfTenMA.getText();
        if (tenMonAn.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên món ăn!");
            return;
        }
        Float donGia;
        try {
            donGia = Float.parseFloat(tfDonGiaMA.getText());
            if (donGia < 0 || donGia > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
                tfDonGiaMA.setText("0");
                return;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!!");
            tfDonGiaMA.setText("0");
            return;
        }
        try {
            MonAn_DTO ma = new MonAn_DTO();
            ma.setMaMonAn(maMonAn);
            ma.setTenMonAn(tenMonAn);
            ma.setDonGia(donGia.toString());
            MonAn_BUS.Update(ma);
            showBangMA();
        } catch (Exception ex) {
            Logger.getLogger(JIFThemMonAn.class.getName()).log(Level.SEVERE, null, ex);
        }
        tfTenMA.setText("");
        tfDonGiaMA.setText("0");
        btXoaMA.setEnabled(false);
        btCapNhatMA.setEnabled(false);
    }//GEN-LAST:event_btCapNhatMAActionPerformed

    private void btXoaMAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaMAActionPerformed
        // TODO add your handling code here:
        int row = this.tbMonAn.getSelectedRow();
        int xoa = JOptionPane.showConfirmDialog(rootPane, "Bạn có chắc chắn muốn xóa?");
        if (xoa != 0) {
            return;
        }
        try {
            String maMonAnXoa = (String) this.tbMonAn.getValueAt(row, 0).toString();
            if(MonAn_BUS.kiemTraMonAnCoTrongCTPhieuDatTiec(maMonAnXoa)){
                JOptionPane.showMessageDialog(rootPane, "Món ăn đã được đặt tiệc không thể xóa!");
                return;
            }
            MonAn_BUS.Delete(maMonAnXoa);
            showBangMA();
        } catch (Exception e) {
        }
        tfTenMA.setText("");
        tfDonGiaMA.setText("0");
        btXoaMA.setEnabled(false);
        btCapNhatMA.setEnabled(false);
    }//GEN-LAST:event_btXoaMAActionPerformed

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhatMA;
    private javax.swing.JButton btThemMA;
    private javax.swing.JButton btThoat;
    private javax.swing.JButton btXoaMA;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable tbMonAn;
    private javax.swing.JTextField tfDonGiaMA;
    private javax.swing.JTextField tfTenMA;
    // End of variables declaration//GEN-END:variables

    private void showBangMA() throws Exception {
        tbMonAn.setModel(MonAn_BUS.layBangMonAn());
    }

    private void luuMonAn() throws Exception {
        String tenMonAn;
        tenMonAn = tfTenMA.getText();
        if (tenMonAn.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên món ăn!");
            return;
        }
        Float donGia;
        try {
            donGia = Float.parseFloat(tfDonGiaMA.getText());
            if (donGia < 0 || donGia > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
                tfDonGiaMA.setText("0");
                return;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
            tfDonGiaMA.setText("0");
            return;
        }
        maMonAn = maMonAn();
        MonAn_DTO ma = new MonAn_DTO();
        ma.setMaMonAn(maMonAn);
        ma.setTenMonAn(tenMonAn);
        ma.setDonGia(donGia.toString());
        MonAn_BUS.Insert(ma);
        JOptionPane.showMessageDialog(rootPane, "Thêm món ăn thành công!");
        showBangMA();
    }

    private String maMonAn() throws Exception {
        String lastID = MonAn_BUS.layMaCuoiCung();
        String nextID = null;
        try {
            if (lastID != null) {
                nextID = MonAn_BUS.taoMaTuDong(lastID, 3);
            } else {
                nextID = MonAn_BUS.taoMaTuDong("MMA0000", 3);
            }
        } catch (Exception e) {
        }
        return nextID;
    }
}
